package com.example.kotlinproject.modules.login

import android.content.Context
import android.widget.Toast
import com.example.kotlinproject.api.WanAndroidAPI
import com.example.kotlinproject.entity.LoginResponse
import com.example.kotlinproject.modules.login.inter.LoginModule
import com.example.kotlinproject.modules.login.inter.LoginPresenter
import com.example.kotlinproject.net.APIClient
import com.example.kotlinproject.net.ApiResponse
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class LoginModuleImpl :LoginModule{
    override fun login(
        context: Context,
        username: String,
        password: String,
        loginListener: LoginPresenter.OnLoginListener
    ) {
        APIClient.instance.instanceRetrofit(WanAndroidAPI::class.java)//被观察者对象
            .loginAction(username,password)
            .subscribeOn(Schedulers.io())//设置上流线程
            .observeOn(AndroidSchedulers.mainThread())//设置下流线程
            /*    .subscribe(object :Observer<LoginResponseWrapper<LoginResponse>>{
                    override fun onComplete() {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                    override fun onSubscribe(d: Disposable?) {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                    override fun onNext(t: LoginResponseWrapper<LoginResponse>?) {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                    override fun onError(e: Throwable?) {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                })*/
            .subscribe(object : ApiResponse<LoginResponse>(context) {
                override fun success(loginResponse: LoginResponse?) {
                    //Toast.makeText(this@LoginActivity,"登录成功", Toast.LENGTH_LONG).show()
                    loginListener.loginSuccess(loginResponse)
                }

                override fun fail(errorMsg: String?) {
                    //Toast.makeText(this@LoginActivity,"登录失败,$errorMsg", Toast.LENGTH_LONG).show()
                    loginListener.loginFialure(errorMsg)
                }
            })


    }

}